<template>
  <el-dialog title="请核对信息" :visible="outerVisible" :before-close="close">
    <el-scrollbar style="height:calc(70vh - 184px)" :wrapStyle="[{'overflow-x':'hidden'}]">
      <el-table :data="tableData" :show-header="false">
        <el-table-column prop="label" label="项目" width="150px"></el-table-column>
        <el-table-column prop="value" label="值"></el-table-column>
      </el-table>
      <el-dialog width="50vh" title="验证密码" :visible.sync="innerVisible" append-to-body>
        <el-input v-model="passwd" placeholder="请输入密码" show-password></el-input>
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerVisible=false">取消</el-button>
          <el-button @click="verify" type="primary">验证</el-button>
        </span>
      </el-dialog>
    </el-scrollbar>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="innerVisible = true">确认提交</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  props: {
    formData: Object,
    outerVisible: Boolean,
  },
  data() {
    return {
      innerVisible: false,
      passwd: "",
    };
  },
  computed: {
    tableData() {
      let userName = "";
      if (this.$store.state.login.userbase) {
        userName = this.$store.state.login.userbase.name;
      }
      let sector = "";
      if (this.formData.seat.length > 1) {
        sector = this.formData.seat[1];
      }
      let partners = new Array();
      for (let i = 0; i < this.formData.partners.length; i++) {
        partners.push(this.formData.partners[i].join("  "));
      }
      return [
        {
          label: "姓名",
          value: this.formData.name,
        },
        {
          label: "资质",
          value: this.formData.rank,
        },
        {
          label: "科室",
          value: this.formData.office,
        },
        {
          label: "班组",
          value: this.formData.group,
        },
        {
          label: "席位",
          value: this.formData.seat[0],
        },
        {
          label: "扇区",
          // value: this.formData.seat[1]
          value: sector,
        },
        {
          label: "搭班人员",
          // value: this.formData.partners.join()
          value: partners.join(" / "),
        },
        {
          label: "典型描述",
          value: this.formData.attributes.join(" / "),
        },
        {
          label: "纠违时间",
          value: this.formData.date,
        },
        {
          label: "纠违项目",
          value: this.formData.item.join(" / "),
        },
        {
          label: "项目分值",
          value: this.formData.value,
        },
        {
          label: "事件描述",
          value: this.formData.description,
        },
        {
          label: "提交选项",
          value: this.formData.level,
        },
        {
          label: "提交人",
          value: userName,
          // value: this.$store.state.login.userbase.name
        },
      ];
    },
    postData() {
      let userName = "";
      if (this.$store.state.login.userbase) {
        userName = this.$store.state.login.userbase.name;
      }
      let userGroup = "";
      if (this.$store.state.login.userinfo) {
        userGroup = this.$store.state.login.userinfo.clientGroup;
      }
      let userOffice = "";
      if (this.$store.state.login.userinfo) {
        userOffice = this.$store.state.login.userinfo.clientOffice;
      }
      let sector = "";
      if (this.formData.length == 1) {
        sector = "";
      } else {
        sector = this.formData.seat[1];
      }
      let partners = new Array();
      for (let i = 0; i < this.formData.partners.length; i++) {
        partners.push(this.formData.partners[i].join("-"));
      }
      return {
        clientName: this.formData.name,
        clientGroup: this.formData.group,
        clientOffice: this.formData.office,
        clientRank: this.formData.rank,
        seat: this.formData.seat[0],
        sector: sector,
        partners: partners.join(","),
        attributes: this.formData.attributes.join(","),
        eventTime: this.formData.date,
        category: this.formData.item[0],
        item: this.formData.item[1],
        value: this.formData.value,
        description: this.formData.description,
        userName: userName,
        userGroup: userGroup,
        userOffice: userOffice,
        level: this.formData.level,
        type: "现场纠违",
      };
    },
  },
  methods: {
    close() {
      this.$emit("cancel");
    },
    verify() {
      if (
        this.$store.state.login.status === 0 &&
        this.passwd === this.$store.state.login.userbase.passwd
      ) {
        // console.log(this.postData);
        this.axios
          .post("api/create_rectify_record/", {
            user: this.$store.state.login.userbase,
            data: this.postData,
          })
          .then((response) => {
            // console.log(response.data);
            this.$store.commit("setResponse", response.data);
            if (response.data.status === 0) {
              //
              this.innerVisible = false;
              this.passwd = "";
              //
              this.$notify.success({
                title: "完成",
                message: "数据提交成功",
              });
              this.$emit("finish");
            } else {
              this.$notify.error({
                title: "错误",
                message: "提交数据时发生错误，请进行纸质记录",
              });
            }
          })
          .catch();
      } else {
        this.$notify.error({
          title: "错误",
          message: "请输入正确的登录密码",
        });
      }
    },
  },
};
</script>